<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2019/2/13
 * Time: 11:18
 */
namespace App\Ordersys\Controllers\Admin;
use App\Base\Controllers\Controller;
use App\Ordersys\Contracts\WxuserInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class WxuserController extends Controller
{

    /**
     * 产品接口
     * @return WxuserInterface
     */
    public function getWxuserInterface()
    {
        return app()->make(WxuserInterface::class);
    }

    /**
     * 广告首页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {

        if($request->getMethod()=='POST'){
            $where=function($query) use ($request){
                $name=$request->input("name");
                if(!empty($name)){
                    $query->where("name","like",'%'.$name.'%');
                }
                $wx_no=$request->input("wx_no");
                if(!empty($wx_no)){
                    $query->where("wx_no","like",'%'.$wx_no.'%');
                }
            };
            return  $this->getWxuserInterface()->getList($where);
        }
        return view("ordersys.wxuser.index");
    }

    /**
     * 广告编辑
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */

    public function edit(Request $request)
    {
        $id=$request->input("id");
        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'name' => 'required',
                'wx_no' => 'required',

            ],["name.required"=>'微信名称必须有值',"wx_no.required"=>'微信号必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return  $this->getWxuserInterface()->saveData($request->all());
        }
        $data=$this->getWxuserInterface()->getInfo($id);
        return view("ordersys.wxuser.edit")->with("data",$data);
    }

    /**
     * 广告添加
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(Request $request)
    {

        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'name' => 'required',
                'wx_no' => 'required',

            ],["name.required"=>'微信名称必须有值',"wx_no.required"=>'微信号必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return  $this->getWxuserInterface()->saveData($request->all());
        }
        return view("ordersys.wxuser.add");
    }

    /**
     * 广告删除
     * @param Request $request
     */
    public function del(Request $request){
        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'id' => 'required',
            ],["id.required"=>'删除参数id是必须的']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $where=function($query) use ($request){
                $id=$request->input("id");
                $query->where("id",$id);
            };
            return  $this->getWxuserInterface()->deleteData($where);
        }
    }

    /**
     * 改变状态
     * @param Request $request
     */
    public function changeStatus(Request $request){
        return  $this->getWxuserInterface()->saveData($request->all());
    }

}
